import { Permission } from 'src/entity/permission.entity';
import { Method } from '../enum';

const insertPermissions = async ({ paths }) => {
  for (const path in paths) {
    if (!path.startsWith('/api')) {
      for (const permission in paths[path]) {
        let newPermission = new Permission();

        let obj = {
          name: paths[path][permission].operationId,
          method: permission,
          path: path.replace('/{id}', ''),
          sort: 0,
          type: 0,
          componentName: null,
        };
        
        newPermission.name = obj.name;

        if (permission == 'get') {
          newPermission.method = Method.GET;
        } else if (permission == 'post') {
          newPermission.method = Method.POST;
        } else if (permission == 'patch') {
          newPermission.method = Method.PATCH;
        } else if (permission == 'delete') {
          newPermission.method = Method.DELETE;
        }

        newPermission.path = obj.path;
        newPermission.sort = obj.sort;
        newPermission.type = obj.type;
        newPermission.componentName = obj.componentName;

        // 插入数据库
        await newPermission.save();
      }
    }
  }
};
export default insertPermissions;
